package com.example.userwallet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.userwallet.entity.Account;
import com.example.userwallet.entity.Wallet;
import com.example.userwallet.mapper.AccountMapper;
import com.example.userwallet.mapper.WalletMapper;
import com.example.userwallet.service.WalletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.userwallet.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 钱包表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-15
 */
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet> implements WalletService {


    @Autowired(required = false)
    AccountMapper accountMapper;
    @Autowired(required = false)
    WalletMapper walletMapper;


    /**
     *
     * @param userAccount 用户账号
     * @return 各种情况返回
     */
    @Override
    public JsonUtil SeekBalanceSer(String userAccount) {
        System.out.println(userAccount);
        JsonUtil jsonUtil = new JsonUtil();
        //判断用户账号是否存在
        List<Account> userList = accountMapper.selectList(
                new QueryWrapper<Account>()
                        .select("user_account")
                        .eq("user_account",userAccount));

        if(userList.size()<=0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("该用户账号不存在");
            return jsonUtil;
        }
        //查询用户钱包余额
       Wallet walletList = walletMapper.selectOne(
               new QueryWrapper<Wallet>()
                       .select("amount")
                       .eq("user_account",userAccount)
                       .eq("status",1)
       );
        if(walletList==null){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("该用户未开通钱包");
            return jsonUtil;
        }
        jsonUtil.setCode(0);
        jsonUtil.setMsg("查询成功");
        jsonUtil.setData(walletList.getAmount());
        return jsonUtil;

    }
}
